package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesSaleExpressRpcService;
import com.hunttown.mes.rpc.domain.MesSaleExpressDTO;
import com.hunttown.mes.rpc.domain.query.MesSaleExpressDTOQuery;
import com.hunttown.mes.service.MesSaleExpressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesSaleExpressConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:34:55
 */
@Service
public class MesSaleExpressRpcServiceImpl implements MesSaleExpressRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesSaleExpressRpcServiceImpl.class);

    private final MesSaleExpressService mesSaleExpressService;

    @Autowired
    public MesSaleExpressRpcServiceImpl(MesSaleExpressService mesSaleExpressService) {
        this.mesSaleExpressService = mesSaleExpressService;
    }

    @Override
    public MesSaleExpressDTO insert(MesSaleExpressDTO mesSaleExpressDTO) {
        try {
            return convert(mesSaleExpressService.insert(convert(mesSaleExpressDTO)));
        } catch (Exception e) {
            logger.error("MesSaleExpress create failed.", e);
            throw new RuntimeException("MesSaleExpress create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesSaleExpressDTO mesSaleExpressDTO) {
        try {
            return mesSaleExpressService.updateInfoById(convert(mesSaleExpressDTO));
        } catch (Exception e) {
            logger.error("MesSaleExpress update by id failed.", e);
            throw new RuntimeException("MesSaleExpress update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesSaleExpressDTOQuery mesSaleExpressDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleExpressDTOQuery.getW_id() != null && mesSaleExpressDTOQuery.getW_id() > 0) {
                query.put("w_id", mesSaleExpressDTOQuery.getW_id());
            }

            if (mesSaleExpressDTOQuery.getIds() != null && !mesSaleExpressDTOQuery.getIds().equals("")) {
                query.put("ids", mesSaleExpressDTOQuery.getIds());
            }

            if (mesSaleExpressDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleExpressDTOQuery.getStopFlag());
            }

            if (mesSaleExpressDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleExpressDTOQuery.getDeleteFlag());
            }

            return mesSaleExpressService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesSaleExpress update by query failed.", e);
            throw new RuntimeException("MesSaleExpress update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleExpressDTO selectById(Integer id) {
        try {
            return convert(mesSaleExpressService.selectById(id));
        } catch (Exception e) {
            logger.error("MesSaleExpress select by id failed.", e);
            throw new RuntimeException("MesSaleExpress select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesSaleExpressDTO selectObjByQuery(MesSaleExpressDTOQuery mesSaleExpressDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesSaleExpressDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesSaleExpressDTOQuery.getStopFlag());
            }

            if (mesSaleExpressDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesSaleExpressDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesSaleExpressDTOQuery.getOrderBy())) {
                query.put("orderBy", mesSaleExpressDTOQuery.getOrderBy());
            }

            return convert(mesSaleExpressService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesSaleExpress select obj by query failed.", e);
            throw new RuntimeException("MesSaleExpress select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesSaleExpressDTO> getListForPage(MesSaleExpressDTOQuery mesSaleExpressDTOQuery) {
        Query query = Query.page(mesSaleExpressDTOQuery.getPn(), mesSaleExpressDTOQuery.getPageSize());

        if (mesSaleExpressDTOQuery.getId() != null && mesSaleExpressDTOQuery.getId() > 0) {
            query.put("id", mesSaleExpressDTOQuery.getId());
        }

        if (mesSaleExpressDTOQuery.getIds() != null && !mesSaleExpressDTOQuery.getIds().equals("")) {
            query.put("ids", mesSaleExpressDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDTOQuery.getLikeName())) {
            query.put("likeName", mesSaleExpressDTOQuery.getLikeName());
        }

        if (mesSaleExpressDTOQuery.getOrderId() != null) {
            query.put("orderId", mesSaleExpressDTOQuery.getOrderId());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDTOQuery.getShowDate())) {
            query.put("showDate", mesSaleExpressDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDTOQuery.getStartDate())) {
            query.put("startDate", mesSaleExpressDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDTOQuery.getEndDate())) {
            query.put("endDate", mesSaleExpressDTOQuery.getEndDate());
        }

        if (mesSaleExpressDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesSaleExpressDTOQuery.getStopFlag());
        }

        if (mesSaleExpressDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesSaleExpressDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesSaleExpressDTOQuery.getOrderBy())) {
            query.put("orderBy", mesSaleExpressDTOQuery.getOrderBy());
        }

        List<MesSaleExpressDTO> list = convert(mesSaleExpressService.selectByQuery(query));
        Page<MesSaleExpressDTO> page = Page.from(mesSaleExpressDTOQuery.getPn(), mesSaleExpressDTOQuery.getPageSize(), list);
        page.setTotalItems(mesSaleExpressService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesSaleExpressService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesSaleExpress delete failed.", e);
            throw new RuntimeException("MesSaleExpress delete failed, Please order your code.", e);
        }
    }
}